package com.guyaning.deyson_ec.ec.main.sort.list;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.guyaning.deyson_core.core.delegates.DeysonDelegate;
import com.guyaning.deyson_core.core.net.RestClient;
import com.guyaning.deyson_core.core.net.callback.ISuccess;
import com.guyaning.deyson_core.core.ui.recycler.MultipleItemBean;
import com.guyaning.deyson_ec.ec.R;
import com.guyaning.deyson_ec.ec.R2;
import com.guyaning.deyson_ec.ec.main.sort.SortDelegate;

import java.util.List;

import butterknife.BindView;

/**
 * Created by Administrator on 2018/6/26.
 */

public class VerticalListDelegate extends DeysonDelegate {

    @BindView(R2.id.rl_vertical_list)
    RecyclerView mRecyclerView = null;

    @Override
    public Object setLayout() {
        return R.layout.delegate_vertical_list;
    }

    private void initRecyclerView() {

        LinearLayoutManager manager = new LinearLayoutManager(getContext());

        mRecyclerView.setLayoutManager(manager);
        //屏蔽动画效果
        mRecyclerView.setItemAnimator(null);
    }

    @Override
    public void onBindView(@Nullable Bundle savedInstanceState, @NonNull View rootView) {
        initRecyclerView();
    }

    //数据懒加载


    @Override
    public void onLazyInitView(@Nullable Bundle savedInstanceState) {
        super.onLazyInitView(savedInstanceState);

        RestClient.builder()
                .url("sort_list.php")
                .loader(getContext())
                .success(new ISuccess() {
                    @Override
                    public void onSuccess(String response) {

                        List<MultipleItemBean> data = new VerticalListDataConverter()
                                .setJson(response)
                                .convert();
                        SortDelegate delegate = getParentDelegate();

                        SortRecyclerAdapter adapter = new SortRecyclerAdapter(data, delegate);

                        mRecyclerView.setAdapter(adapter);
                    }
                })
                .build()
                .get();
    }
}
